package com.allenkerr;

import com.allenkerr.common.Constants;
import com.allenkerr.model.User;
import com.allenkerr.dao.UserRepository;
import com.mongodb.WriteResult;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MongoDbTest01 {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Test
    public void testSave() {
        //先删除
        userRepository.deleteAll();

        //创建用户
        User u1 = new User("1", "user1", "12345", "张三", 22, Constants.MAN);
        User u2 = new User("2", "user2", "12345", "翠花", 23, Constants.WOMAN);
        User u3 = new User("3", "user3", "12345", "蛋蛋", 21, Constants.MAN);

        //保存
        userRepository.save(u1);
        userRepository.save(u2);
        userRepository.save(u3);
    }

    @After
    public void testFindAll() {
        List<User> users = userRepository.findAll();
        users.forEach(System.out::println);
    }

    @Test
    public void testDelete() {
        User user = new User();
        user.setId("1");
        Example<User> example = Example.of(user);
        User userInDB = userRepository.findOne(example);
        System.out.println("userInDB = " + userInDB);

        //删除
        userRepository.delete(userInDB);
    }

    /**
     * 按照 age 排序查询结果
     */
    @Test
    public void testFind() {
        Sort.Order order = new Sort.Order(Sort.Direction.ASC, "age");
        Sort sort = new Sort(order);
        List<User> users = userRepository.findAll(sort);
        users.forEach(System.out::println);
        System.out.println();
    }

    @Test
    public void testUpdate1() {
        Query query = Query.query(Criteria.where("id").is("4"));

        Update update = Update.update("password", "abcdefg")
                .set("salary", 1999.99)
                .set("address", "HongKong");

        User userToModify = mongoTemplate.findAndModify(query, update, User.class);
        System.out.println("userToModify = " + userToModify);
    }

    @Test
    public void testUpdate2() {
        Query query = Query.query(Criteria.where("id").is("4"));

        Update update = Update.update("password", "789456")
                .set("salary", 2999.99)
                .set("address", "ShangHai");

        FindAndModifyOptions options = FindAndModifyOptions.options().returnNew(true);

        User modifiedUser = mongoTemplate.findAndModify(query, update, options, User.class);
        System.out.println("modifiedUser = " + modifiedUser);
    }

    @Test
    public void testUpdate3() {
        Query query = Query.query(Criteria.where("id").is("3"));

        Update update = Update.update("password", "456123")
                .set("salary", 9999.99)
                .set("address", "BeiJing");

        WriteResult result = mongoTemplate.updateFirst(query, update, User.class);
        System.out.println("result = " + result);
    }

}
